package nio;


import java.io.*;
import java.util.Arrays;
import java.util.List;

import org.apache.http.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
/**
 * @program JavaBase
 * @description:
 * @author: zhanglu
 * @create: 2020-03-04 10:18:00
 */
public class ServerFile {

    public static void main(String[] args) {

        String url = "http://localhost:8001/file/test";
        String root = "/Users/zhanglu/Desktop/luImg";
        List<String> files = Arrays.asList(root + "/1.jpg", root + "/100.jpeg");

        CloseableHttpClient httpclient = HttpClients.createDefault();
        //CloseableHttpClient httpclient = HttpClientBuilder.create().build();
        try {
            HttpPost httppost = new HttpPost(url);

            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(200000).setSocketTimeout(200000).build();
            httppost.setConfig(requestConfig);

            FileBody bin = new FileBody(new File(files.get(0)));
            StringBody comment = new StringBody("This is comment", ContentType.TEXT_PLAIN);

//            HttpEntity reqEntity = MultipartEntityBuilder.create().build().addPart("file", bin).addPart("comment", comment).build();

            MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();

            files.stream().forEach(o -> multipartEntityBuilder.addPart("file", new FileBody(new File(o))));

            multipartEntityBuilder.addPart("username", new StringBody("Lu", ContentType.TEXT_PLAIN))
                                  .addPart("password", new StringBody("111111", ContentType.TEXT_PLAIN));


            HttpEntity reqEntity = multipartEntityBuilder.build();

            httppost.setEntity(reqEntity);

            System.out.println("executing request " + httppost.getRequestLine());
            CloseableHttpResponse response = httpclient.execute(httppost);
            try {
                System.out.println(response.getStatusLine());
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    String responseEntityStr = EntityUtils.toString(response.getEntity());
                    System.out.println(responseEntityStr);
                    System.out.println("Response content length: " + resEntity.getContentLength());
                }
                EntityUtils.consume(resEntity);
            } finally {
                response.close();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                httpclient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }


}
